package catchData.notWellDone;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.httpclient.Cookie;
import org.apache.commons.httpclient.HostConfiguration;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;

import po.MyObject;

/**
 * @author suwy
 * 2013-1-4
 * TODO catch data from guidechem
 * should be concerned about how to get more ips from outside
 */
public class CatchATC4guidechem {

	private static HttpClient client;
	private static final String LOGIN_PAGE = "http://china.guidechem.com/datacenter/atc_list-p1.html";
	private static final String HOST = "china.guidechem.com";
	private static int pages = 244;
	private static List<MyObject> ipList = new ArrayList<MyObject>();
	
	private void initHttpClient(List<MyObject> ipList, int seq) {
			MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
			client = new HttpClient(connectionManager);
			client.getParams().setCookiePolicy(
					CookiePolicy.BROWSER_COMPATIBILITY);
			client.getParams().setParameter(
					"http.protocol.single-cookie-header", true);
			client.getParams().setParameter(
					HttpMethodParams.HTTP_CONTENT_CHARSET, "gb2312");
			client.getHttpConnectionManager().getParams().setConnectionTimeout(300000);
			client.getHttpConnectionManager().getParams().setSoTimeout(300000);
			HostConfiguration config = client.getHostConfiguration();
			config.setProxy(ipList.get(seq).getObj1(), ipList.get(seq).getNum1());
	}
	
	private void setHeaders(HttpMethod method) {
		method.getParams().setParameter("http.protocol.cookie-policy", CookiePolicy.BROWSER_COMPATIBILITY);
		method.setRequestHeader("accept","text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
		method.setRequestHeader("referer", LOGIN_PAGE);
		method.setRequestHeader("accept-encoding", "GBK,utf-8");
		method.setRequestHeader("accept-language", "zh-CN,zh;q=0.8");
		method.setRequestHeader("content-type", "application/x-www-form-urlencoded");
		method.setRequestHeader("user-agent","Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; staticlogin:product=cboxf2010&act=login&info=ZmlsZW5hbWU9UG93ZXJXb3JkMjAxME94Zl9VbHRpbWF0ZS5leGUmbWFjPUY4RDgxQUY2MTgwQjQ3REQ5MUI5QkM4NjU3MzAwRkU3JnBhc3Nwb3J0PSZ2ZXJzaW9uPTIwMTAuNi4zLjYuMiZjcmFzaHR5cGU9MQ==&verify=b226e2d473bb8dbbb37568db14fee0e4; staticlogin:product=cboxf2010&act=login&info=ZmlsZW5hbWU9UG93ZXJXb3JkMjAxME94Zl9TcGVjaWFsLmV4ZSZtYWM9RjhEODFBRjYxODBCNDdERDkxQjlCQzg2NTczMDBGRTcmcGFzc3BvcnQ9JnZlcnNpb249MjAxMC42LjMuNS4yJmNyYXNodHlwZT0x&verify=94c65611a34cfb4964638346b072d76e; staticlogin:product=cboxf09&act=login&info=ZmlsZW5hbWU9UG93ZXJ3b3JkMjAwOU94Zi4yNTI2OS40MDExLmV4ZSZtYWM9RjhEODFBRjYxODBCNDdERDkxQjlCQzg2NTczMDBGRTcmcGFzc3BvcnQ9JnZlcnNpb249MjAwOS4wNS4yNS4zLjI3MiZjcmFzaHR5cGU9MQ==&verify=32e210faa8ce04f90dfc4225f98e80ac; aff-kingsoft-ciba; TheWorld)");
		method.setRequestHeader("host", HOST);
		method.setRequestHeader("connection", "Keep-Alive");
		method.setRequestHeader("cache-control", "no-cache");
		method.setRequestHeader("cookie", getCookieString());
	}
	
	public String getNow() {
		return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
	}
	
	private void saveDataFile(String sFolder, String data, int isidd) {
		File folder = new File(sFolder+"/"+sFolder);
		if (!folder.exists()) folder.mkdirs();
		PrintWriter out = null;
		String saveFile = sFolder + "/" + isidd + ".htm";
		try {
			out = new PrintWriter(new OutputStreamWriter(new FileOutputStream(saveFile), "utf-8"));
			out.println(data);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (out != null) {
					out.close();
					out = null;
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			System.out.println(getNow() + saveFile.replaceAll("\\\\", "/"));
		}
	}
	
	private String getCookieString() {
		Cookie[] cookies = client.getState().getCookies();
		StringBuilder builder = new StringBuilder();
		for (Cookie cookie : cookies) {
			builder.append(cookie.getDomain()).append(":").append(
					cookie.getName()).append("=").append(cookie.getValue())
					.append(";").append(cookie.getPath()).append(";").append(
							cookie.getExpiryDate()).append(";").append(
							cookie.getSecure()).append(";/n");
		}
		return builder.toString();
	}
	
	private void setIps(String ip, Integer port) {
		MyObject o = new MyObject();
		o.setObj1(ip);
		o.setNum1(port);
		ipList.add(o);
	}
	
	private void getIps() {
		setIps("219.234.82.82", 8888);
		setIps("177.43.213.46",3128);
		setIps("187.84.177.11",8080);
		setIps("189.39.120.5",3128);
	}
	
	private void excute() {
		getIps();
		try {
			for(int i = 1; i<= pages; i++){
				if (i%10 == 0 || (i == 1)) {
					int seq = i/10;
					initHttpClient(ipList, seq);
				}
//				initHttpClient();
				PostMethod method = new PostMethod("http://china.guidechem.com/datacenter/atc_list-p"+i+".html");
				setHeaders(method);
				client.executeMethod(method);
				String str = method.getResponseBodyAsString();
				saveDataFile("E:/downloadData/ATC", str, i);
				method.releaseConnection();
			}
			}catch (HttpException e) {
					e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		CatchATC4guidechem instance = new CatchATC4guidechem();
		instance.excute();
	}
}